<?php

namespace app\system\controller\daily;

use app\admin\controller\order\Throwable;
use app\common\controller\Backend;
use think\facade\Db;

/**
 * 订单列管理
 */
class Suborder extends Backend {
    /**
     * Order模型对象
     * @var object
     * @phpstan-var \app\admin\model\order\Order
     */
    protected object $model;

    protected array|string $preExcludeFields = ['id', 'create_time'];

    protected string|array $quickSearchField = ['out_trade_no', 'user.username'];

    public function initialize(): void {
        parent::initialize();
        $this->model = new \app\system\model\daily\Suborder;
    }

    /**
     * 更细订单状态
     */
    public function updateStatus(): void{
        $param = $this->request->param();
        db::name('daily_suborder')->where(['id' => $param['id']])->update(['status' => $param['current_status']]);
        $row = $this->model->field($this->indexField)->withJoin($this->withJoinTable, $this->withJoinType)->where(['id' => $param['id']])->find();
        $this->success('', [
            'row' => $row
        ]);
    }


    /**
     * 查看
     * @throws Throwable
     */
    public function index(): void {
        $this->request->filter(['strip_tags', 'trim']);

        $param = $this->request->param();
//        print_r($param);die;

        if ($this->request->param('select')) {
            $this->select();
        }
        $this->withJoinTable = ['goods'];
        list($where, $alias, $limit, $order) = $this->queryBuilder();
        $res = $this->model
            ->field($this->indexField)
            ->withJoin($this->withJoinTable, $this->withJoinType)
            ->alias($alias)
            ->where($where)
            ->order($order)
            ->paginate($limit);

        $list = $res->items();

        foreach ($list as $val) {
//            $val->money = number_format($val->money);
            $val->create_time = date('Y-m-d H:i:s', $val->create_time);
        }

        $this->success('', [
            'list' => $list,
            'total' => $res->total(),
            'remark' => get_route_remark(),
        ]);
    }




    /**
     * 若需重写查看、编辑、删除等方法，请复制 @see \app\admin\library\traits\Backend 中对应的方法至此进行重写
     */
}